package com.example.chatroom.configuration;

import com.example.chatroom.controller.WebSocketAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private WebSocketAPI webSocketAPI;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 该方法把刚才创建好的Handler类注册到具体路径上
        // 当浏览器websocket的请求路径为注册路径时，就会自动调用到webSocketAPI里的方法
        registry.addHandler(webSocketAPI,"/WebSocketMessage")
                // 通过注册这个特定的HttpSession拦截器，实现HttpSession和WebSocketSession共享会话属性
                // 就可把用户添加到HttpSession里添加的键值对添加至WebSocketSession
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
